package com.uchengwang.ui.base_ui.basefm;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.cycle.adapter.CycleListAdapter;
import com.uchengwang.ui.cm_ui.cycle.adapter.CycleMainAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleNoticeBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.cycle.ui.CycleListActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.MsgNoticyActivity;
import com.uchengwang.ui.cm_ui.umsg.adapter.MsgTopAdapter;
import com.uchengwang.ui.cm_ui.umsg.bean.MsgTopBean;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/4/19.
 * 新社圈主页
 */

public class FragmentCycleMain extends BaseFragment implements View.OnClickListener{
    private View view;

    private RecyclerView rv, fmMain_tuijian_rv;
    private CycleMainAdapter adapter;
    private ActivityTitleBar titleBar;
    private long reFreshTime;//刷新时间

    private TextView cycle_no_login, follow_main_name, follow_main_desc, fmMain_no_cycles_tv;

    private ImageView follow_main_head;
    private RelativeLayout follow_main_rl, fmMain_rv_rl;
    private LinearLayout fmMain_add_ln, fmMain_no_cycles;

    private List<CycleBean> localCycleBeans;

    private PtrClassicFrameLayout refresh;
    private RecyclerAdapterWithHF mAdapter;
    private GridView fmMain_gv;
    private MsgTopAdapter msgTopAdapter;
    private CycleListAdapter tuijianAdapter;

    private LinearLayout fmMain_tuijian_ln;//推荐社圈

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fragment_cycle_main);
        view = super.onCreateView(inflater, container, savedInstanceState);

        EventBus.getDefault().register(this);
        initView();
        return view;
    }

    private void initView() {
        fmMain_gv = view.findViewById(R.id.fmMain_gv);
//        refresh = view.findViewById(R.id.fmMain_refresh);

        titleBar = view.findViewById(R.id.fmMain_titleBar);
        titleBar.setTitle("Uni", R.color.white);
        titleBar.getTitleBar_bg().setBackground(null);

        fmMain_add_ln = view.findViewById(R.id.fmMain_add_ln);
        fmMain_add_ln.setOnClickListener(this);

        titleBar.setImageLeft(R.drawable.icon_search_white, 96, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/search");
                intent.putExtra(BaseConfig.IS_FULL, true);
                startActivity(intent);
            }
        });

        titleBar.setImageRight(R.drawable.club_release_msg_no, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(FragmentCycleMain.this.getContext(), MsgNoticyActivity.class));
                titleBar.showOrHideRightDot(View.GONE);
            }
        });
        rv = view.findViewById(R.id.fmMain_rv);


        fmMain_tuijian_ln = view.findViewById(R.id.fmMain_tuijian_ln);
        fmMain_tuijian_rv = view.findViewById(R.id.fmMain_tuijian_rv);//推荐社圈

//        cycle_no_login = view.findViewById(R.id.cycle_no_login);
//        cycle_no_login.setOnClickListener(this);

        follow_main_rl = view.findViewById(R.id.follow_main_rl);
        follow_main_rl.setOnClickListener(this);

        follow_main_name = view.findViewById(R.id.follow_main_name);
        follow_main_desc = view.findViewById(R.id.follow_main_desc);

        follow_main_head = view.findViewById(R.id.follow_main_head);

        fmMain_rv_rl = view.findViewById(R.id.fmMain_rv_rl);//有社圈时显示

        fmMain_no_cycles = view.findViewById(R.id.fmMain_no_cycles);//无社圈时显示
        fmMain_no_cycles_tv = view.findViewById(R.id.fmMain_no_cycles_tv);
        fmMain_no_cycles_tv.setOnClickListener(this);


        if (UCommonUtil.getUtil().isLogin()){
            initData();
        }else {
            rv.setVisibility(View.GONE);
//            follow_main_rl.setVisibility(View.GONE);
//            room_main_rl.setVisibility(View.GONE);
//            cycle_no_login.setVisibility(View.VISIBLE);
        }

    }

    private void initData(){
        reFreshTime = System.currentTimeMillis();

        List<MsgTopBean> list = new ArrayList<>();

        list.add(new MsgTopBean("享聊", R.drawable.icon_xiangliao));
        list.add(new MsgTopBean("享玩", R.drawable.icon_xiangwan));
        list.add(new MsgTopBean("会场", R.drawable.icon_room_image));
        msgTopAdapter = new MsgTopAdapter(getContext(),list);
        fmMain_gv.setAdapter(msgTopAdapter);


        localCycleBeans = new ArrayList<>();
//        localCycleBeans.add(new CycleBean("享聊", "陪伴你关心的", R.drawable.icon_xiangliao));
//        localCycleBeans.add(new CycleBean("享玩", "伙伴一起玩起来", R.drawable.icon_xiangwan));
//        localCycleBeans.add(new CycleBean("会场", "互动, 投票, 影集, 抽奖, 名片", R.drawable.icon_room_image));



        adapter = new CycleMainAdapter(getContext());
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.setData(localCycleBeans);
        rv.setAdapter(adapter);

        tuijianAdapter = new CycleListAdapter(getContext());
        tuijianAdapter.setShow(true);
        fmMain_tuijian_rv.setLayoutManager(new LinearLayoutManager(getContext()));
        fmMain_tuijian_rv.setAdapter(tuijianAdapter);
//        mAdapter = new RecyclerAdapterWithHF(adapter);



        getRefresh();

//        refresh.setAutoLoadMoreEnable(true);
//        refresh.setLoadMoreEnable(false);
//
//        refresh.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                refresh.autoRefresh(true);
//            }
//        }, 150);
//
//        refresh.setPtrHandler(new PtrDefaultHandler() {
//
//            @Override
//            public void onRefreshBegin(PtrFrameLayout frame) {
//                getRefresh();
//            }
//        });
//
//        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
//
//            @Override
//            public void loadMore() {
//            }
//        });


    }

    @Override
    public void onResume() {
        super.onResume();
        if (UCommonUtil.getUtil().isLogin()){
            ImageManager.getInstance().loadImage(getContext(), follow_main_head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
        }

        if ((System.currentTimeMillis()- reFreshTime)/1000/60 >5){
            getRefresh();
        }

    }

    //获取更新信息
    private void getRefresh(){
        reFreshTime = System.currentTimeMillis();
        CycleHttpUtils.getUtils().checkFollowNew(getActivity(), true, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (!TextUtils.isEmpty(data)){
//                    follow_main_desc.setText("新动态");
                    follow_main_desc.setVisibility(View.VISIBLE);
//                    follow_main_desc.setTextColor(getResources().getColor(R.color.color_red));
                    ((MainActivity)getActivity()).showDot(0);
                }else {
//                    follow_main_desc.setTextColor(getResources().getColor(R.color.comment_color));
//                    follow_main_desc.setText("暂无新动态");
                    follow_main_desc.setVisibility(View.GONE);
                }
            }
        });

        CycleHttpUtils.getUtils().getCycleNotice(getActivity(), 1, 1, true, false, new OnHttpResultListener<CycleNoticeBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleNoticeBean> list) {
                if (list != null){
                    titleBar.showOrHideRightDot(View.VISIBLE);
                }
            }
        });

        getCreateAndJoinCycles();

        getRecommendCycles();

    }

    /**
     * 获取自己创建或者加入的社圈
     * */
    public void getCreateAndJoinCycles(){
        CycleHttpUtils.getUtils().getCreateAndJoinCycles(getActivity(), UCommonUtil.getUtil().getId(), 0, new OnHttpResultListener<CycleBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleBean> list) {
//                refresh.refreshComplete();
                if (list != null && list.size() >0){
                    fmMain_rv_rl.setVisibility(View.VISIBLE);
                    fmMain_no_cycles.setVisibility(View.GONE);
                    List<CycleBean> allCycle = new ArrayList<>();
//                    allCycle.addAll(localCycleBeans);
                    allCycle.addAll(list);
                    adapter.setData(allCycle);
                }else {
                    fmMain_rv_rl.setVisibility(View.GONE);
                    fmMain_no_cycles.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    /**
     * 获取推荐的社圈
     * */
    private void getRecommendCycles(){
        CycleHttpUtils.getUtils().getRecommendCycles(getActivity(), new OnHttpResultListener<CycleBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleBean> list) {
                if (list != null && list.size() >0){
                    fmMain_tuijian_ln.setVisibility(View.VISIBLE);
                    tuijianAdapter.setData(list);
                }else {
                    fmMain_tuijian_ln.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 收到EventBus信息
     * */
    public void onEvent(CycleBean cycleBean) {
        if (cycleBean != null){
            long cycleId = cycleBean.getId();
            List<CycleBean> list = tuijianAdapter.getData();
            for (CycleBean bean: list){
                if (bean.getId() ==cycleId){
                    bean.setIsjoin(0);
                    tuijianAdapter.notifyDataSetChanged();
                    break;
                }
            }

        }
    }

    @Override
    public void onClick(View v) {
        if (UCommonUtil.getUtil().isNeedLogin(getActivity(), false)){
            return;
        }
        Intent intent = null;
        switch (v.getId()){
            case R.id.fmMain_add_ln:
            case R.id.fmMain_no_cycles_tv:
                startActivity(new Intent(FragmentCycleMain.this.getContext(), CycleListActivity.class));
                break;
            case R.id.follow_main_rl:
                intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.PUB_TYPE, 1);
                intent.putExtra(BaseConfig.URL_TITLE, "关注");
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/shares");
                ((MainActivity)getActivity()).hideDot(0);
//                follow_main_desc.setTextColor(getResources().getColor(R.color.comment_color));
//                follow_main_desc.setText("暂无新动态");
                follow_main_desc.setVisibility(View.GONE);
                break;
        }
        if (intent != null){
            startActivity(intent);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
